﻿using System;
using System.Collections.Generic;
using System.Linq;
using Colligate;

namespace OrgDataSource
{
    public class OUDS: IOrgDS
    {
        #region\\delete
        public int DeleteTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, orgEntities context)
        {
            return 0;
        }
        #endregion
        #region\\get
        public System.Xml.XmlNode GetTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, orgEntities context, BaseTaskDataSource baseInfo)
        {
            switch (DataType.ToLower())
            {
                case "relateoulist": return GetRelateOUList(DataGuid, context);
                case "searchouinfo": return GetSearchOUInfo(DataGuid, context);
                default: throw new Exception("OUDS无法处理该" + DataType + "的数据");
            }
        }

        private static System.Xml.XmlNode GetRelateOUList(string DataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), DataGuid, "RelateOUList");
                var subNode = XmlNodeManage.CreateNode("ListOUDataInfo");
                var searchInfo = GetSearchOUInfo(DataGuid, context);
                var ouType=XmlNodeManage.GetSubValue(searchInfo.FirstChild, "@OUType");
                switch (ouType)
                {
                    case "Company": CompanyInfo(DataGuid, subNode, context); break;
                    case "Branch": BranchInof(DataGuid, subNode, context); break;
                    case "Member": MemberInfo(DataGuid, subNode, context); break;
                    case "Application": ApplicationInfo(DataGuid, subNode, context); break;
                    case "Group": GroupInfo(DataGuid, subNode, context); break;
                }
                result.AppendChild(subNode);
                return result;
            }
            catch
            {
                throw new Exception("获取的SearchOUInfo数据时出错！");
            }
        }
        private static void CompanyInfo(string id, System.Xml.XmlNode subNode,orgEntities context)
        {
            var childNode = XmlNodeManage.CreateNode("OUDataInfo");
            var company = context.Company.FirstOrDefault(cc => cc.Guid == id);
            XmlNodeManage.SetSubValue(childNode, "@GUID", company.Guid);
            XmlNodeManage.SetSubValue(childNode, "@NAME", company.Name);
            XmlNodeManage.SetSubValue(childNode, "@OUType", "Company");
            XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", company.Guid);
            subNode.AppendChild(childNode);
            var branchList = context.Branch.Where(cc => cc.CompanyGuid == id);
            foreach (var item in branchList)
            {
                var branchNode = XmlNodeManage.CreateNode("OUDataInfo");
                XmlNodeManage.SetSubValue(childNode, "@GUID", item.Guid);
                XmlNodeManage.SetSubValue(childNode, "@NAME", item.Name);
                XmlNodeManage.SetSubValue(childNode, "@OUType", "Branch");
                XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", item.CompanyGuid);
                subNode.AppendChild(branchNode);
            }
            GetGroup(id, context, subNode);
        }
        private static void BranchInof(string id, System.Xml.XmlNode subNode,orgEntities context)
        {
            var childNode = XmlNodeManage.CreateNode("OUDataInfo");
            var branch = context.Branch.FirstOrDefault(cc => cc.Guid == id);
            XmlNodeManage.SetSubValue(childNode, "@GUID", branch.Guid);
            XmlNodeManage.SetSubValue(childNode, "@NAME", branch.Name);
            XmlNodeManage.SetSubValue(childNode, "@OUType", "Branch");
            XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", branch.CompanyGuid);
            subNode.AppendChild(childNode);
            var branchList = new List<Branch>();
            GetBranch(id, context, branchList);
            foreach (var item in branchList)
            {
                var branchNode = XmlNodeManage.CreateNode("OUDataInfo");
                XmlNodeManage.SetSubValue(childNode, "@GUID", item.Guid);
                XmlNodeManage.SetSubValue(childNode, "@NAME", item.Name);
                XmlNodeManage.SetSubValue(childNode, "@OUType", "Branch");
                XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", item.CompanyGuid);
                subNode.AppendChild(branchNode);
            }
            GetGroup(id, context, subNode);
        }
        private static void MemberInfo(string id, System.Xml.XmlNode subNode, orgEntities context)
        {
            var idList = new List<string> {id};
            var childNode = XmlNodeManage.CreateNode("OUDataInfo");
            var member = context.Member.FirstOrDefault(cc => cc.Guid == id);
            XmlNodeManage.SetSubValue(childNode, "@GUID", member.Guid);
            XmlNodeManage.SetSubValue(childNode, "@NAME", member.Name);
            XmlNodeManage.SetSubValue(childNode, "@OUType", "Member");
            XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", member.Guid);
            //XmlNodeManage.AddSubValue()
            subNode.AppendChild(childNode);
            var branch = context.Branch.FirstOrDefault(cc => cc.MasterGuid == id);
            if (null!=branch)
            {
                idList.Add(branch.Guid);
                var branchNode = XmlNodeManage.CreateNode("OUDataInfo");
                XmlNodeManage.SetSubValue(branchNode, "@GUID", branch.Guid);
                XmlNodeManage.SetSubValue(branchNode, "@NAME", branch.Name);
                XmlNodeManage.SetSubValue(branchNode, "@OUType", "Branch");
                XmlNodeManage.SetSubValue(branchNode, "@CompanyGuid", branch.CompanyGuid);
                subNode.AppendChild(branchNode);
            }
            var company = context.Company.FirstOrDefault(cc => cc.EventerGuid == id);
            if (null != company)
            {
                idList.Add(company.Guid);
                var companyNode = XmlNodeManage.CreateNode("OUDataInfo");
                XmlNodeManage.SetSubValue(companyNode, "@GUID", company.Guid);
                XmlNodeManage.SetSubValue(companyNode, "@NAME", company.Name);
                XmlNodeManage.SetSubValue(companyNode, "@OUType", "Company");
                XmlNodeManage.SetSubValue(companyNode, "@CompanyGuid", company.Guid);
                subNode.AppendChild(companyNode);
            }
            foreach (var item in idList)
                GetGroup(item, context, subNode);
        }
        private static void ApplicationInfo(string id, System.Xml.XmlNode subNode, orgEntities context)
        {
            var childNode = XmlNodeManage.CreateNode("OUDataInfo");
            var application = context.Application.FirstOrDefault(cc => cc.Guid == id);
            XmlNodeManage.SetSubValue(childNode, "@GUID", application.Guid);
            XmlNodeManage.SetSubValue(childNode, "@NAME", application.Name);
            XmlNodeManage.SetSubValue(childNode, "@OUType", "Application");
            XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", application.CompanyGuid);
            subNode.AppendChild(childNode);
            GetGroup(id, context, subNode);
        }
        private static void GroupInfo(string id, System.Xml.XmlNode subNode, orgEntities context)
        {
            var childNode = XmlNodeManage.CreateNode("OUDataInfo");
            var group = context.Group.FirstOrDefault(cc => cc.Guid == id);
            XmlNodeManage.SetSubValue(childNode, "@GUID", group.Guid);
            XmlNodeManage.SetSubValue(childNode, "@NAME", group.Name);
            XmlNodeManage.SetSubValue(childNode, "@OUType", "Group");
            XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", group.Guid);
            subNode.AppendChild(childNode);
        }
        private static void GetBranch(string id, orgEntities context, List<Branch> branchList)
        {
            var result = context.Branch.Where(cc => cc.ParentGuid == id).ToList();
            if (result.Count != 0)
            {
                branchList.AddRange(result);
                foreach (var item in result)
                {
                    GetBranch(item.Guid, context, branchList);
                }
            }
        }
        private static void GetGroup(string id, orgEntities context, System.Xml.XmlNode subNode)
        {
            var groupList = context.Group.Where(cc => cc.GroupMember.Contains(context.GroupMember.FirstOrDefault(dd => dd.MemberGuid == id)));
            foreach (var item in groupList)
            {
                var groupNode = XmlNodeManage.CreateNode("OUDataInfo");
                XmlNodeManage.SetSubValue(groupNode, "@GUID", item.Guid);
                XmlNodeManage.SetSubValue(groupNode, "@NAME", item.Name);
                XmlNodeManage.SetSubValue(groupNode, "@OUType", "Group");
                XmlNodeManage.SetSubValue(groupNode, "@CompanyGuid", item.Guid);
                subNode.AppendChild(groupNode);
            }
        }

        private static System.Xml.XmlNode GetSearchOUInfo(string DataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), DataGuid, "SearchOUInfo");
                var subNode = XmlNodeManage.CreateNode("OUDataInfo");
                var tempInfo=GetInfo(DataGuid,context);
                var guid=tempInfo["Guid"]??string.Empty;
                var name=tempInfo["Name"]??string.Empty;
                var ouType=tempInfo["OUType"]??string.Empty;
                var companyGuid=tempInfo["CompanyGuid"]??string.Empty;
                XmlNodeManage.SetSubValue(subNode, "@GUID", guid);
                XmlNodeManage.SetSubValue(subNode, "@NAME", name);
                XmlNodeManage.SetSubValue(subNode, "@OUType", ouType);
                XmlNodeManage.SetSubValue(subNode, "@CompanyGuid", companyGuid);
                result.AppendChild(subNode);
                return result;
            }
            catch
            {
                throw new Exception("获取的SearchOUInfo数据时出错！");
            }
        }
        private static Dictionary<string, string> GetInfo(string id,orgEntities context)
        {
            var result = new Dictionary<string, string>();
            var company = context.Company.FirstOrDefault(cc => cc.Guid == id);
            if (null != company)
            {
                result.Add("Guid", id);
                result.Add("Name", company.Name);
                result.Add("OUType", "Company");
                result.Add("CompanyGuid", id);
                return result;
            }
            var branch = context.Branch.FirstOrDefault(cc => cc.Guid == id);
            if(null!=branch)
            {
                result.Add("Guid", id);
                result.Add("Name", branch.Name);
                result.Add("OUType", "Branch");
                result.Add("CompanyGuid", branch.CompanyGuid);
                return result;
            }
            var member = context.Member.FirstOrDefault(cc => cc.Guid == id);
            if (null != member)
            {
                result.Add("Guid", id);
                result.Add("Name", member.Name);
                result.Add("OUType", "Member");
                result.Add("CompanyGuid", Utility.GetCompanyGuid(member.BranchGuid, context.Branch.ToArray()));
                return result;
            }
            var application = context.Application.FirstOrDefault(cc => cc.Guid == id);
            if (null != application)
            {
                result.Add("Guid", id);
                result.Add("Name", application.Name);
                result.Add("OUType", "Application");
                result.Add("CompanyGuid", application.CompanyGuid);
                return result;
            }
            var group = context.Group.FirstOrDefault(cc => cc.Guid == id);
            if (null != group)
            {
                result.Add("Guid", id);
                result.Add("Name", group.Name);
                result.Add("OUType", "Group");
                result.Add("CompanyGuid", id);
                return result;
            }
            return result;
        }
        #endregion
        #region\\set
        public int SetTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, System.Xml.XmlNode XmlData, orgEntities context)
        {
            throw new NotImplementedException();
        }
        #endregion
        #region\\transform
        public System.Xml.XmlNode TransformTaskData(OUDataInfo UserInfo, System.Xml.XmlNode xmlParam, orgEntities context, BaseTaskDataSource baseInfo)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
